/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.query;

import com.google.gwt.user.client.rpc.IsSerializable;

import nfse.gui.util.GUIUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;

public class QueryOptions implements IsSerializable {

    public String queryID = "";

    public int queryType = -1;

    public String ts = "";

    public Date startDate = null;

    public Date endDate = null;

    private HashMap<Integer,Boolean> types = new HashMap<Integer,Boolean>();

    public String srcIP1 = "";

    public String srcIP2 = "";

    public int srcIPPred = -1;

    public String dstIP1 = "";

    public String dstIP2 = "";

    public int dstIPPred = -1;

    public int dstPort1 = -1;

    public int dstPort2 = -1;

    public int dstPortPred = -1;

    public String ips = "";

    public int ipSearchType = -1;

    public String note = "";

    public int limit = 1000000;

    public QueryOptions() {

    }

    public int[] getTypes() {
        Collection<Integer> col = types.keySet();
        int[] temp = new int[col.size()];
        Iterator<Integer> it = col.iterator();
        for (int i = 0; i < temp.length; i++)
            temp[i] = it.next();
        return temp;
    }

    public void setType(int type, boolean selected) {
        types.put(new Integer(type), new Boolean(selected));
    }

    public boolean isSelected(int type) {
        Object temp = types.get(new Integer(type));
        if (temp == null)
            return false;
        return ((Boolean) temp).booleanValue();
    }

    public String getIPsString() {
        String temp = "";
        if (queryType != GUIUtil.QUERY_TYPE_IPS)
            return temp;

        temp += ips;
        if (ipSearchType == GUIUtil.IP_SEARCH_AS_BOTH)
            temp += " (as Src and Dst)";
        else if (ipSearchType == GUIUtil.IP_SEARCH_AS_SRC)
            temp += " (as Src)";
        else if (ipSearchType == GUIUtil.IP_SEARCH_AS_DST)
            temp += " (as Dst)";

        return temp;
    }

    public String getDstIPString() {
        String temp = "";
        if (dstIP1.length() == 0 || queryType != GUIUtil.QUERY_TYPE_SEARCH)
            return temp;

        if (dstIPPred == GUIUtil.PREDICATE_EQUALS) {
            temp = "Dst IP = " + dstIP1;
        } else {
            temp = "Dst IP " + dstIP1 + " to " + dstIP2;
        }
        return temp;
    }

    public String getSrcIPString() {
        String temp = "";
        if (srcIP1.length() == 0 || queryType != GUIUtil.QUERY_TYPE_SEARCH)
            return temp;

        if (srcIPPred == GUIUtil.PREDICATE_EQUALS) {
            temp = "Src IP = " + srcIP1;
        } else {
            temp = "Src IP " + srcIP1 + " to " + srcIP2;
        }
        return temp;
    }

    public String getDstPortString() {
        String temp = "";
        if (dstPort1 == -1 || queryType != GUIUtil.QUERY_TYPE_SEARCH)
            return temp;

        if (dstPortPred == GUIUtil.PREDICATE_EQUALS) {
            temp = "Dst Port = " + dstPort1;
        } else {
            temp = "Dst Port " + dstPort1 + " to " + dstPort2;
        }
        return temp;
    }

    public String getSearchString() {
        if (queryType != GUIUtil.QUERY_TYPE_SEARCH)
            return "";

        String temp = "";
        String srcIPStr = getSrcIPString();
        String dstIPStr = getDstIPString();
        String dstPortStr = getDstPortString();

        if (srcIPStr.length() > 0)
            temp += srcIPStr;

        if (dstIPStr.length() > 0) {
            if (temp.length() > 0)
                temp += ", ";
            temp += dstIPStr;
        }

        if (dstPortStr.length() > 0) {
            if (temp.length() > 0)
                temp += ", ";
            temp += dstPortStr;
        }

        return temp;
    }

    public String toXML() {
        StringBuilder temp = new StringBuilder("<SearchOptions");
        temp.append(" queryid=\"" + queryID + "\"");
        temp.append(" querytype=\"" + queryType + "\"");
        temp.append(" dstip1=\"" + dstIP1 + "\"");
        temp.append(" dstip2=\"" + dstIP2 + "\"");
        temp.append(" srcip1=\"" + srcIP1 + "\"");
        temp.append(" srcip2=\"" + srcIP2 + "\"");
        temp.append(" dstport1=\"" + dstPort1 + "\"");
        temp.append(" dstport2=\"" + dstPort2 + "\"");
        temp.append(" dstippred=\"" + dstIPPred + "\"");
        temp.append(" srcippred=\"" + srcIPPred + "\"");
        temp.append(" dstportpred=\"" + dstPortPred + "\"");
        temp.append(" limit=\"" + note + "\"");
        temp.append(" note=\"" + note + "\"");
        temp.append(" timestamp=\"" + ts + "\"");
        temp.append(" startts=\"" + startDate + "\"");
        temp.append(" endts=\"" + startDate + "\"");
        temp.append(" ips=\"" + ips + "\"");
        temp.append(" ipsearchtype=\"" + ipSearchType + "\"");
        temp.append(">");

        int[] types = getTypes();
        if (types != null) {
            for (int i = 0; i < types.length; i++) {
                temp.append("<Type id=\"" + types[i] + "\"/>");
            }
        }
        temp.append("</SearchOptions>");
        return temp.toString();
    }

}
